package com.water.app.Impl.account;

import com.water.app.interfaces.account.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.Arrays;

@Service
public class EmailServiceImpl implements EmailService {

    private final JavaMailSender mailSender;

    @Autowired
    public EmailServiceImpl(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }
    @Override
    public int sendEmail(String to) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("2797857024@qq.com");
        //邮箱接收者
        message.setTo(to);
        //邮箱主题
        String subject = "验证码";
        message.setSubject(subject);
        // 生成 1000 到 9999 之间的随机整数，确保是四位数
        int code = 1000 + (int)(Math.random() * 9000);
        //邮箱内容
        String text = "你的验证码是：" +code;
        message.setText(text);
        mailSender.send(message);
        return code;
    }

    @Override
    public boolean sendEmail(String to, String address,String name) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("2797857024@qq.com");
        //邮箱接收者
        message.setTo(to);
        //邮箱主题
        String subject = "您的订单已送达";
        message.setSubject(subject);
        //邮箱内容
        String text = "尊敬的"+name+"先生,您的订单已经送到：" +address+",收到请确认收货哦~";
        message.setText(text);
        mailSender.send(message);
        return true;
    }
}
